//
//  RawArticleView.m
//  Final
//
//  Created by student14 on 2021/12/19.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "RawArticleView.h"

@interface RawArticleView ()

@property(strong, nonatomic) NSString* rawArticle;
@property(strong, nonatomic) NSString* a_id;
@property(strong, nonatomic) UIViewController* svc;

@property(strong, nonatomic) UILabel* label;
@property(strong, nonatomic) UIGestureRecognizer* tapGesture;

@end

@implementation RawArticleView

-(RawArticleView*)initWithRawArticle: (NSString*)ra
                        andArticleID: (NSString*)a_id
                   andViewController: (UIViewController*)svc {
    self = [super init];
    self.rawArticle = [NSString stringWithFormat:@"%@......", ra];
    self.a_id = a_id;
    self.svc = svc;
    
    self.label.text = self.rawArticle;
    [self addSubview:self.label];
    
    [self addGestureRecognizer:self.tapGesture];
    
    return self;
}

- (void) resetRawArticle:(NSString *)ra
            andArticleID: (NSString*)a_id
       andViewController:(UIViewController*)svc {
    self.rawArticle = [NSString stringWithFormat:@"%@......", ra];
    self.label.text = self.rawArticle;
    self.a_id = a_id;
    self.svc = svc;
}

//Lazy load
-(UILabel*)label
{
    if(_label == nil){
//        _label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, self.svc.view.frame.size.width-60, 30)];
        _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 354, 30)];
        [_label setFont:[UIFont systemFontOfSize:20]];
        [_label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"gray.png"]]];
        [_label setTextColor:[UIColor blackColor]];
    }
    return _label;
}

-(UIGestureRecognizer*)tapGesture
{
    if(_tapGesture == nil){
        _tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapArticle)];
    }
    return _tapGesture;
}

-(void)tapArticle
{
    NSLog(@"Tap the article!");
    Article *detailArticle = [[ModelController getInstance] getArticleWithAID:self.a_id];
    DetailViewController *dvc = [[DetailViewController alloc] initWithArticle:detailArticle];
    [_svc.navigationController pushViewController:dvc animated:YES];
}

@end
